﻿#region

using System;
using System.Linq;
using System.Web.Mvc;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;

#endregion

namespace VinhSon.Controllers
{
    public class PageArticleController : BasePageController
    {
        //
        // GET: /Page/PageArticle/

        public ActionResult Index(int id)
        {
            Article article = UnitOfWork.ArticleRepository.Select(c => c.Id == id,
                                                                  "Category,Sections")
                                        .FirstOrDefault();
            if(article != null)
                if(!(article.Inactive ?? false))
                {
                    ViewBag.CategoryArticles = UnitOfWork.ArticleRepository.Select(c => c.Category.Id == article.Category.Id && c.Activated == true && (c.Inactive == null || c.Inactive == false) && c.Id != id)
                                                         .OrderByDescending(c => c.LastModified)
                                                         .Take(10)
                                                         .ToList();

                    int status = article.Status ?? 0;
                    if((status & ArticleModel.SHOW_AS_PHOTO_GALERRY) == ArticleModel.SHOW_AS_PHOTO_GALERRY)
                    {
                        return View("PhotoGalerry",
                                    article);
                    }
                    return View(article);
                }
            return Redirect("/Trang-Chu");
        }

        public ActionResult PhotoGalerry(int id)
        {
            Article article = UnitOfWork.ArticleRepository.Select(c => c.Id == id,
                                                                  "Category,Sections")
                                        .FirstOrDefault();
            if(article != null)
                if(!(article.Inactive ?? false))
                {
                    ViewBag.CategoryArticles = UnitOfWork.ArticleRepository.Select(c => c.Category.Id == article.Category.Id && c.Activated == true && (c.Inactive == null || c.Inactive == false) && c.Id != id)
                                                         .OrderByDescending(c => c.LastModified)
                                                         .Take(10)
                                                         .ToList();

                    return View(article);
                }
            return Redirect("/Trang-Chu");
        }

        public ActionResult RightColumn()
        {
            return PartialView(UnitOfWork.GroupRepository.Select(c => (c.FromDate == null || c.FromDate <= DateTime.Now) && (c.ToDate == null || c.ToDate >= DateTime.Now) && c.ArticlePage == true && c.Activated == true && (c.Inactive == false || c.Inactive == null),
                                                                 "Layout"));
        }
    }
}
